#include "device.h"
#include <string.h>
#include "cbb_bit.h"

Device_t*
device_register(void* pvFeature){

    // 设备数量已满或者参数错误
    if(NULL == pvFeature) return NULL;
    
    // 申请设备结构体
    Device_t* pstDevice = DEV_MALLOC(sizeof(Device_t));
    if(NULL == pstDevice) return NULL;

    // 初始化设备结构体
    pstDevice->pvFeature      = pvFeature;
    pstDevice->u32Event       = 0;

    // 返回设备结构体
    return pstDevice;
}

uint8_t 
get_device_event(Device_t* pstDevice, uint8_t bit){

    // 输入参数检查
    if(NULL == pstDevice || MAX_EVENT_BITS <= bit) return 0;

    // 获取事件位
    uint8_t u8Ret = cbb_get_bit(&pstDevice->u32Event, BIT_32,bit);

    // 返回事件位
    return u8Ret;
}

DEV_RET_t 
clear_device_event(Device_t* pstDevice, uint8_t bit){

    // 输入参数检查
    if(NULL == pstDevice || MAX_EVENT_BITS <= bit) return DEV_PARAM_ERR;

    // 清除事件位
    cbb_clear_bit(&pstDevice->u32Event,BIT_32, bit);

    // 返回事件位
    return DEV_OK;
}

DEV_RET_t 
set_device_event(Device_t* pstDevice, uint8_t bit){

    // 输入参数检查
    if(NULL == pstDevice || MAX_EVENT_BITS <= bit) return DEV_PARAM_ERR;

    // 设置事件位
    cbb_set_bit(&pstDevice->u32Event, BIT_32, bit);

    // 返回事件位
    return DEV_OK;
}
